# coding:utf-8
from odoo import models, fields, api, _
from odoo.osv import expression


class OLAnalyticLine(models.Model):
    _inherit = 'account.analytic.line'

    so_origin = fields.Char(u'SO订单', compute='get_origin', store=True)
    invoice_partner_id = fields.Many2one('res.partner', string=u'客户名称', related="move_id.partner_id")

    @api.multi
    @api.depends('ref', 'move_id')
    def get_origin(self):
        for al in self:
            if al.move_id.invoice_id.origin:
                al.so_origin = al.move_id.invoice_id.origin
            else:
                if al.ref:
                    pickings = self.env["stock.picking"].search([("name", "=", al.ref)])
                    for picking in pickings:
                        if picking.origin.startswith('SO'):
                            al.so_origin = picking.origin
                            continue
                        else:
                            pickings2 = self.env["stock.picking"].search([("name", "=", picking.origin)])
                            for picking2 in pickings2:
                                if picking2.origin.startswith('SO'):
                                    al.so_origin = picking2.origin
                                    continue
